import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-10
 * Time: 8:44
 */
public class MySQLInsert {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/lib?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入条数:");
        int num = scanner.nextInt();
        System.out.println("请输入书名id:");
        int book_id = scanner.nextInt();
        System.out.println("请输入学生id:");
        int student_id =scanner.nextInt();
        //吸收回车
        String c = scanner.nextLine();
        System.out.println("请输入借阅开始时间:");
        String start_time = scanner.nextLine();
        //吸收回车
        //String c1 = scanner.nextLine();
        System.out.println("请输入借阅结束时间:");
        String end_time = scanner.nextLine();

        String sql = "insert into borrow_info values(?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,num);
        statement.setInt(2,book_id);
        statement.setInt(3,student_id);
        statement.setString(4,start_time);
        statement.setString(5,end_time);
        System.out.println("statement="+ statement );

        int n = statement.executeUpdate();
        System.out.println("n="+n);

        statement.close();
        connection.close();
    }
}
